package com.awspaas.user.apps.shhtaerospaceindustrial.visiting;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.util.DBSql;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

@Controller
public class AppCmdCancelVisitApply {
	
	/*
	 * 加载用户提交申请审批完成的所有院部展厅参观申请
	 */
	@Mapping("com.awspaass.user.apps.LoadVisitApplyDetails")
	public static String getLoadVisitApplyDetails(UserContext uc,String cgbegin,String cgend,String ydperson) {
		JSONObject result = new JSONObject();
		String checksql="";
		String sql="";
		if(!"".equals(cgbegin)) {//参观时间大于等于
			sql = " and t.VISITDATE >='" + cgbegin + "' ";
			checksql +=sql;
		}
		if(!"".equals(cgend)) { //参观时间小于等于
			sql =" and t.VISITDATE <='" + cgend + "' ";
			checksql +=sql;
		}
		if(!"".equals(ydperson)) { //申请人
			sql =" and t.TARGETMAN like '%" + ydperson + "%' ";
			checksql +=sql;
		}
		try {
			if(!"".equals(checksql)) {
				String querySql = "select t.* from  VIEW_VISIT_CANCLEAPPLY  t  where t.BUSISTATUS in ('2') "
						+ checksql + " order by  t.visitdate  asc ";
				List<Map<String, Object>> urlList = DBSql.query(querySql, new ColumnMapRowMapper(), new Object[] {});
				if (urlList == null || urlList.isEmpty()) {
					JSONArray itemArray = new JSONArray();
					result.put("status", "0");
					result.put("item", itemArray);
				}
				JSONArray itemArray = new JSONArray();
				for (int i = 0; i < urlList.size(); i++) {
					Map<String, Object> urlMap = urlList.get(i);
					JSONObject item = new JSONObject();
					String TARGETDEPT = CoreUtil.objToStr(urlMap.get("TARGETDEPT"));// 申请部门
					String TARGETMAN = CoreUtil.objToStr(urlMap.get("TARGETMAN"));// 申请人
					String TARGETMANPHONE = CoreUtil.objToStr(urlMap.get("TARGETMANPHONE"));// 联系电话
					String BUSISTATUS = CoreUtil.objToStr(urlMap.get("BUSISTATUS"));// 状态
					String BINDID = CoreUtil.objToStr(urlMap.get("BINDID"));// 流程实例id
					StringBuffer num = new StringBuffer("");
					if(!"".equals(BINDID)) {
						String bodySql = "select t.*  from  BO_EU_INNLOC_VISIT_APP_LOCS  t  where  "
								+ "t.BINDID =  '"+ BINDID +"' order by  t.visitdate  asc ";
						List<Map<String, Object>> bodyList = DBSql.query(bodySql, new ColumnMapRowMapper(), new Object[] {});
						for (int j = 0; j < bodyList.size(); j++) {
							Map<String, Object> bodyMap = bodyList.get(j);
							String TARGETUNIT = CoreUtil.objToStr(bodyMap.get("TARGETUNIT"));// 参观单位
							String LOCATIONNAME = CoreUtil.objToStr(bodyMap.get("LOCATIONNAME"));// 地点名称
							String VISITDATE = CoreUtil.objToStr(bodyMap.get("VISITDATE"));// 预计参观日期
							String VISITDATETIME = CoreUtil.objToStr(bodyMap.get("VISITDATETIME"));// 参观开始时间
							num.append(TARGETUNIT+"-"+LOCATIONNAME+"-"+VISITDATE+"-"+VISITDATETIME+"\n");
						}
					}
					item.put("targetdept", TARGETDEPT);
					item.put("targetman", TARGETMAN);
					item.put("targetmanphone", TARGETMANPHONE);
					item.put("busistatus", BUSISTATUS);
					item.put("bindid", BINDID);
					item.put("visitaddress", num.toString());
					itemArray.add(item);
					itemArray.add(item);
					
				}
				result.put("status", "0");
				result.put("item", itemArray);
			}else {
			Date now = new Date();// 系统日期
			SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String today = DateFormat.format(now);// 日期
			Calendar calendar = Calendar.getInstance();  
		    calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 6);  
		    Date endtoday = calendar.getTime();  
		    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
		    String enddate = format.format(endtoday);  
			String querySql = "select t.* from  VIEW_VISIT_CANCLEAPPLY  t  where t.BUSISTATUS in ('2') "
					+ " and  t.visitdate >=  '"+ today +"' order by  t.visitdate  asc ";
			List<Map<String, Object>> urlList = DBSql.query(querySql, new ColumnMapRowMapper(), new Object[] {});
			if (urlList == null || urlList.isEmpty()) {
				JSONArray itemArray = new JSONArray();
				result.put("status", "0");
				result.put("item", itemArray);
			}
			JSONArray itemArray = new JSONArray();
			for (int i = 0; i < urlList.size(); i++) {
				Map<String, Object> urlMap = urlList.get(i);
				JSONObject item = new JSONObject();
				String TARGETDEPT = CoreUtil.objToStr(urlMap.get("TARGETDEPT"));// 申请部门
				String TARGETMAN = CoreUtil.objToStr(urlMap.get("TARGETMAN"));// 申请人
				String TARGETMANPHONE = CoreUtil.objToStr(urlMap.get("TARGETMANPHONE"));// 联系电话
				String BUSISTATUS = CoreUtil.objToStr(urlMap.get("BUSISTATUS"));// 状态
				String BINDID = CoreUtil.objToStr(urlMap.get("BINDID"));// 流程实例id
				StringBuffer num = new StringBuffer("");
				if(!"".equals(BINDID)) {
					String bodySql = "select t.*  from  BO_EU_INNLOC_VISIT_APP_LOCS  t  where  "
							+ "t.BINDID =  '"+ BINDID +"' order by  t.visitdate  asc ";
					List<Map<String, Object>> bodyList = DBSql.query(bodySql, new ColumnMapRowMapper(), new Object[] {});
					for (int j = 0; j < bodyList.size(); j++) {
						Map<String, Object> bodyMap = bodyList.get(j);
						String TARGETUNIT = CoreUtil.objToStr(bodyMap.get("TARGETUNIT"));// 参观单位
						String LOCATIONNAME = CoreUtil.objToStr(bodyMap.get("LOCATIONNAME"));// 地点名称
						String VISITDATE = CoreUtil.objToStr(bodyMap.get("VISITDATE"));// 预计参观日期
						String VISITDATETIME = CoreUtil.objToStr(bodyMap.get("VISITDATETIME"));// 参观开始时间
						num.append(TARGETUNIT+"-"+LOCATIONNAME+"-"+VISITDATE+"-"+VISITDATETIME+"\n");
					}
				}
				item.put("targetdept", TARGETDEPT);
				item.put("targetman", TARGETMAN);
				item.put("targetmanphone", TARGETMANPHONE);
				item.put("busistatus", BUSISTATUS);
				item.put("bindid", BINDID);
				item.put("visitaddress", num.toString());
				itemArray.add(item);
			}
			result.put("status", "0");
			result.put("item", itemArray);
		  }
		} catch (Exception e) {
			result.put("status", "1");
			result.put("message", e.getMessage());
			e.printStackTrace();
		}

		return result.toString();
	}
	/*
	 * 取消已申请完毕的院部展厅申请
	 * 0:未提交|1:已提交|2:已接单|3:已取消
	 */
	@Mapping("com.awspaass.user.apps.visitApplyCancelAssign")
	public String visitApplyCancelAssign(String bindid) {
		JSONObject returnData = new JSONObject();
		if(null == bindid || "".equals(bindid)  ) {
			returnData.put("status", "0");
			returnData.put("message", "请检查参数!");
			return returnData.toString();
		}
		try {
			String updateAssignSql = "update BO_EU_INNERLOCATION_VISIT_APP set BUSISTATUS = '3' where BINDID = '" + bindid + "'";
			DBSql.update(updateAssignSql);
			returnData.put("status", "0");
			returnData.put("message", "取消成功！");
		} catch (Exception e) {
			returnData.put("status", "1");
			returnData.put("message", e.getMessage());
			e.printStackTrace();

		}
		return returnData.toString();

	}
}
